<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiResponse;
use App\Http\Controllers\Controller;
use App\UserCoupon;
use Carbon\Carbon;
use Illuminate\Http\Request;

class UserCouponController extends Controller
{
    use ApiResponse;
    public $uid;
    public function __construct()
    {
        $this->uid=auth('api')->id();
    }
    public function index(Request $request)
    {
        $amount=$request->get('amount');
        $page=$request->get('page');
        $status=$request->get('status',0);
      $result=UserCoupon::where('uid',$this->uid)->when($amount,function ($query)use ($amount){
          return $query->where('fill_money','<=',$amount);
      })->orderBy("price",'desc')->latest()->where('status',$status);

      if (!$page){
          $result=$result->get();
      }else{
          $result=$result->paginate($request->get('size',10));
      }

      $result->transform(function ($item, $key){
            $item["ck"]=false;

        return $item;
      });
      return $this->data($result);
    }
}
